package com.compunit.composite.test;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class JarHandlingSample {

	public static void main(String[] args) {
		final String path = "/com/compunit/composite/test/data/Stamper.jar";
		final URL url = JarHandlingSample.class.getResource(path);
		final URLClassLoader loader = new URLClassLoader(new URL[] {url});
		final String classpath = "com.compunit.composite.a.Stamper";
		try {
			final Class<?> c = loader.loadClass(classpath);
			System.out.println(c);
			final JarFile jar = new JarFile(new File(url.toURI()));
			final Enumeration<JarEntry> entries = jar.entries();
			while(entries.hasMoreElements()) {
				final JarEntry entry = entries.nextElement();
				final String full = entry.toString().replace("/", ".");
				final String classname = full.substring(0, full.lastIndexOf("."));
				System.out.println("Classname:"+classname);
				// Entry url.
				final URL entryURL = new URL(url, entry.toString());
				System.out.println("EntryURL: " + entryURL);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}
}
